package se.rende.myrss.rome;

import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.feed.rss.Source;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.impl.ConverterForRSS20;

/**
 * This is a convertor for RSS 2.0 setting source on output items
 */
public class ConverterForRSS20WithSource extends ConverterForRSS20 {

    /**
     * Default Constructor
     */
    public ConverterForRSS20WithSource() {
        this("rss_2.0_withSource");
    }

    /**
     * Constructor with type
     * @param type
     */
    protected ConverterForRSS20WithSource(String type) {
        super(type);
    }

    /** 
     * @see com.sun.syndication.feed.synd.impl.ConverterForRSS094#createRSSItem(com.sun.syndication.feed.synd.SyndEntry)
     */
    @Override
    protected Item createRSSItem(SyndEntry sEntry) {
        Item item = super.createRSSItem(sEntry);
        if(sEntry.getSource() != null) {
            Source s = new Source();
            if (isNotBlank(sEntry.getSource().getUri())) {
            	s.setUrl(sEntry.getSource().getUri());
            }
            s.setValue(sEntry.getSource().getTitle());
            item.setSource(s);
        }

        return item;
    }

	private boolean isNotBlank(String s) {
		return s != null && s.trim().length() > 0;
	}
}